﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.IO;
using System.Threading.Tasks;

namespace ThresholdAssistant.Tools
{
    /// <summary>
    /// Json转换工具类
    /// </summary>
    public static class JsonConvertUtil
    {
        /// <summary>
        /// 将文件流转换为对戏那个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="stream">文件流</param>
        /// <returns></returns>
        public static async Task<T> DeserializeObject<T>(this Stream stream)
        {
            T result = default;
            if (stream == null)
                return result;

            using StreamReader streamReader = new StreamReader(stream);
            var postJson = await streamReader.ReadToEndAsync();
            return JsonConvert.DeserializeObject<T>(postJson);
        }

        /// <summary>
        /// 反序列化请求参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonValue"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(this string jsonValue)
        {
            var result = default(T);
            try
            {
                if (string.IsNullOrEmpty(jsonValue))
                {
                    return result;
                }

                //设置返回数据
                result = JsonConvert.DeserializeObject<T>(jsonValue);
            }
            catch (Exception)
            {
                //序列化转换失败，直接转对象
                result = jsonValue.ToObject<T>();
            }
            return result;
        }

        /// <summary>
        /// 序列化请求参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="paramters"></param>
        /// <returns></returns>
        public static string SerializeObject<T>(this T paramters)
        {
            string jsonValue = "";
            if (paramters == null)
            {
                return jsonValue;
            }

            if (typeof(T).Name.Equals("string", StringComparison.CurrentCultureIgnoreCase))
            {
                //如果是字符串则直接转换成string
                jsonValue = paramters.ToString();
            }
            else
            {
                var jsonConverter = new IsoDateTimeConverter();
                jsonValue = JsonConvert.SerializeObject(paramters, Formatting.Indented, jsonConverter);
            }
            return jsonValue;
        }

        /// <summary>
        /// 反序列化请求参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonValue"></param>
        /// <returns></returns>
        public static T ToDeserializeObject<T>(this string jsonValue)
        {
            var result = default(T);
            try
            {
                if (string.IsNullOrEmpty(jsonValue))
                {
                    return result;
                }

                //设置返回数据
                result = JsonConvert.DeserializeObject<T>(jsonValue);
            }
            catch (Exception)
            {

            }
            return result;
        }
    }
}